<?php

class RealityListing extends AbstractBox {
	
	protected $template = 'reality/listing.tpl';
	
	protected $pageList = array();
	protected $pageUidList = array();
	protected $onPage = 6;
	
	protected function setup() {
		MySmarty::assign('actualPage', $GLOBALS['TSFE']->page);
		
		$pageRepository = new PageRepository();
		$actualPage = $pageRepository->getOneByUid(intval($GLOBALS['TSFE']->page['uid']));
		if ($actualPage) {
			$this->pageList[] = $actualPage;
			$this->pageUidList[] = $actualPage['uid'];
		}
		$this->getAllPagesRecursive(array($GLOBALS['TSFE']->page['uid']));
		
		$realityRepository = new RealityRepository();
		$realityRepository->setLimit($this->onPage);
		$page = 1;
		if (Utils::get('page')) {
			$page = intval(Utils::get('page'));
		}
		MySmarty::assign('page', $page);
		$realityRepository->setPage($page);
		
		$order = array('price' => 'ASC');
		if (Utils::get('sorting') == 'datum') {
			$order = array('crdate' => Utils::get('way') == 'desc' ? 'DESC' : 'ASC');
		} elseif (Utils::get('sorting') == 'cena') {
			$order = array('price' => Utils::get('way') == 'desc' ? 'DESC' : 'ASC');
		}
		$realityRepository->addOrderBy($order);

		if (Utils::get('price_from')) {
			$realityRepository->addAdditionalWhere(array('column' => 'price', 'value' => intval(Utils::get('price_from')), 'comparator' => '>='));
		}
		if (Utils::get('price_to')) {
			$realityRepository->addAdditionalWhere(array('column' => 'price', 'value' => intval(Utils::get('price_to')), 'comparator' => '<='));
		}
		
		if (Utils::get('area_from')) {
			$realityRepository->addAdditionalWhere(array('column' => 'area', 'value' => intval(Utils::get('area_from')), 'comparator' => '>='));
		}
		if (Utils::get('area_to')) {
			$realityRepository->addAdditionalWhere(array('column' => 'area', 'value' => intval(Utils::get('area_to')), 'comparator' => '<='));
		}
		
		$realityList = $realityRepository->getByPid($this->pageUidList);
		$count = $realityRepository->getCountByPid($this->pageUidList);
		
		if ($count == 1) {
			// ak by mal byt v listingu len jeden zaznam pojdeme rovno na detail
			$realita = $realityList[0];
			Utils::redirect($realita->getUrl());
		}
		
		$pages = array();
		$pageCount = ceil($count / $this->onPage);
		if ($pageCount > 1) {
			for ($i = 1; $i <= $pageCount; $i++) {
				$pages[] = $i;
			}
		}
		MySmarty::assign('pageCount', $pageCount);
		MySmarty::assign('pages', $pages);
		MySmarty::assign('actualState', Utils::get());
		MySmarty::assign('realityList', $realityList);
		MySmarty::assign('actualUrlWithoutGetParameters', Utils::getActualUrlWithoutGetParameters());
	}
	
	protected function getAllPagesRecursive($parentList) {
		$pageRepository = new PageRepository();
		$pages = $pageRepository->getByPid($parentList);
		
		if ($pages) {
			$pageUidList = array();
			foreach ($pages as $page) {
				$this->pageList[] = $page;
				$this->pageUidList[] = $page['uid'];
				$pageUidList[] = $page['uid'];
			}
			$this->getAllPagesRecursive($pageUidList);
		}
	}
}

?>